package com.sanuo.pico.act.work;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sanuo.pico.App;
import com.sanuo.pico.adapter.WarnAdapter;
import com.sanuo.pico.databinding.ActWarnBinding;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.Flow1;
import com.sanuo.pico.event.Temp1;
import com.sanuo.pico.event.WarnEvent;
import com.sanuo.pico.utils.Command;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class WarnAct extends BaseWorkActivity {
    ActWarnBinding binding;
    WarnAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ignore = App.getApp().isIgnore();
        binding = ActWarnBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> {
            finish();
        });

        if (getIntent().getBooleanExtra("setting", false)) {
            binding.hide.setVisibility(View.INVISIBLE);
        } else {
            binding.hide.setVisibility(View.VISIBLE);
            binding.hide.setOnClickListener(v -> Command.sendTouchEvent(0x10, 0));
        }

        binding.list.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WarnAdapter(this);
        binding.list.setAdapter(adapter);

        onMessageEvent(Event.temp1);
        onMessageEvent(Event.flow1);

        for (int j = 0; j < Event.warn.getValue().length; j++) {
            Integer i = Event.warn.getValue()[j];
            if (i != null && i == 1) {
                Command.reportWarn(j + 36);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Flow1 event) {
        binding.num3.setText(event.getValue() + "r/s");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Temp1 event) {
        binding.num1.setText(event.getValue() + "℃");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(WarnEvent event) {
        adapter.notifyDataSetChanged();
    }

    boolean ignore;

    @Override
    protected void onResume() {
        super.onResume();
        App.getApp().setIgnore(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getApp().setIgnore(ignore);
    }
}
